﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Project.Model;
using Project.BLL;
using Newtonsoft.Json;
using System.Web.UI;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;
using System.Web.ModelBinding;
using System.Web.Http.Cors;

namespace Project.API.Controllers
{
    [ApiAuthorize]
    public class UsersController : ApiController
    {
        // GET: api/Users
        /// <summary>
        /// 分页获取用户数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页条数</param>
        /// <param name="name">模糊查询字段</param>
        /// <returns>分页数据</returns>
        public ResultPageList<UsersDto> Get(int page,int limit,string name="")
        {
            int count = 0;
            var list = new UsersBll().GetUsersByPage(name, page, limit, out count);

            var data = from u in list
                       select new UsersDto
                       {
                           UserID = u.UId,
                           UserName = u.UName,
                           Sex = u.Sex,
                           Birthday = u.Birthday,
                           Phone = u.Phone,
                           ImgID = u.ImgID,
                           Path= u.Images.Path,
                           LoginName = u.loginname,
                           LoginPwd = u.loginpwd,
                           Status = u.Status,
                           RoleID = u.RId,
                           RoleName = u.Roles.RName,
                           DeptID = u.Roles.DId,
                           DeptName = u.Roles.Department.DName,
                           LastTime = u.LastTime
                       };

            ResultPageList<UsersDto> result = new ResultPageList<UsersDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();

            return result;
        }

        // GET: api/Users/5
        //[ApiActionMonitor]
        public ResultData<UsersDto> Get(int id)
        {
            Users u = new UsersBll().GetUsersOne(id);

            UsersDto data = new UsersDto()
            {
                UserID = u.UId,
                UserName = u.UName,
                Sex = u.Sex,
                Birthday = u.Birthday,
                Phone = u.Phone,
                ImgID = u.ImgID,
                Path = u.Images.Path,
                LoginName = u.loginname,
                LoginPwd = u.loginpwd,
                Status = u.Status,
                RoleID = u.RId,
                RoleName = u.Roles.RName,
                DeptID = u.Roles.DId,
                DeptName = u.Roles.Department.DName,
                LastTime = u.LastTime
            };

            ResultData<UsersDto> result = new ResultData<UsersDto>();
            result.code = data != null ? 1 : 0;
            result.msg = data != null ? "ok！" : "error！";
            result.data = data;
            return result;
        }

        // POST: api/Users
        public Result Post([FromBody] UsersFormDto value)
        {
     
            Users user = new Users();
            user.UId = value.UserID;
            user.UName = value.UserName;
            user.ImgID = value.ImgID;
            user.Phone = value.Phone;
            user.Birthday=value.Birthday;
            user.Status = value.Status;
            user.Sex = value.Sex;
            user.RId = value.RoleID;
            user.loginname  = value.LoginName;
            user.loginpwd = MD5Helper.Md5(value.LoginPwd, "X2");

            user.LastTime = value.LastTime;


            bool bol = new UsersBll().Add(user);

            Result result = new Result();
            result.code = bol?1:0;
            result.msg = bol ? "用户信息添加成功！":"error";
            return result;
        }

        // PUT: api/Users/5
        public Result Put([FromBody] UsersFormDto value)
        {
            //value.LoginPwd = MD5Helper.Md5(value.LoginPwd, "X2");

            Users user = new Users();
            user.UId = value.UserID;
            user.UName = value.UserName;
            user.Phone = value.Phone;
            user.Birthday = value.Birthday;
            user.Status = value.Status;
            user.Sex = value.Sex;
            user.ImgID = value.ImgID;
            user.RId = value.RoleID;
            user.loginname = value.LoginName;
            user.loginpwd = value.LoginPwd;
            user.LastTime = value.LastTime;

            bool bol = new UsersBll().Update(user);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息成功！" : "出错了！";
            return result;
        }


        [Route("Api/Users/EditOldPwd")]
        public Result PutOld(int id,string pwd)
        {
            string LoginPwd = MD5Helper.Md5(pwd, "X2");

            bool bol = new UsersBll().EditOldPwd(id, LoginPwd);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "旧密码正确！" : "旧密码错误！";
            return result;
        }

        [Route("Api/Users/EditNewPwd")]
        public Result PutNew(int id, string pwd)
        {
            string LoginPwd = MD5Helper.Md5(pwd, "X2");

            bool bol = new UsersBll().EditNewPwd(id, LoginPwd);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "密码修改成功！" : "密码修改失败！";
            return result;
        }
        // DELETE: api/Users/5
        public Result Delete(int id)
        {
            bool bol = new UsersBll().DeleteUser(id);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息删除成功！" : "出错了！";
            return result;
        }

        [HttpDelete]
        public Result Delete([FromBody] int[] value)
        {
            bool bol = new UsersBll().DeleteUser(value);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息删除成功！" : "出错了！";
            return result;
        }
        //public Result Get(string pwd)
        //{
        //    pwd = MD5Helper.Md5(pwd, "X2");

        //    UsersDto users = Session["User"] as UsersDto;
        //    Result result = new Result();
        //    result.code = bol ? 1 : 0;
        //    result.msg = bol ? "用户信息删除成功！" : "出错了！";
        //    return result;
        //}
    }
}
